<?php
//默认不接受什么的话是hello world!
if(empty($_GET)){
    echo -1;
	exit;
}
include ('config.php');
include 'checkpostandget.php';
function sel_sql($table,$sel_field,$where,$alert=0){
	global $BIAOTOU;
	$sql="select $sel_field from $BIAOTOU$table where $where";
	if($alert==1){
	    echo $sql;
	}
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			if(strpos($sel_field,",") or $sel_field=="*"){
			    $arr=$row;
			}
			else{
			    $arr=$row[$sel_field];
			}
		}
	}
	return $arr;	
}

function update_sql($table,$field_arr,$where,$xs,$alert=0){ //$xs 0表示更新 1表示同加  2表示同减,$alert默认为0,1表示打印sql语句,2表示先执行在打印
    global $BIAOTOU;
	$sql="";
	foreach($field_arr as $k=>$v){
		if($xs==0){
		    $sql="`$k`='$v',".$sql;
		}
		elseif($xs==1){
		    $sql="`$k`=`$k`+'$v',".$sql;
		}
		elseif($xs==2){
		    $sql="`$k`=`$k`-'$v',".$sql;
		}
	}
	$sql=substr($sql,0,strlen($sql)-1);
	$sql="update $BIAOTOU$table set $sql where $where;";
	if($alert==0){
		mysql_query($sql);
	}
	elseif($alert==1){
		echo $sql;
	}
	elseif($alert==2){
		mysql_query($sql);
	    echo $sql;
	}
}


function insert_one_sql($table,$field_arr,$alert=0){
    global $BIAOTOU;
	$field="";
	$values="";
	foreach($field_arr as $k=>$v){
		$field = "`".$k."`,".$field;
		$values = "'".$v."',".$values;
		}
	$field=substr($field,0,strlen($field)-1);
	$values=substr($values,0,strlen($values)-1);
	$sql="insert into $BIAOTOU$table($field) values ($values);";
	/*$fp = fopen('url.txt', 'a');
fwrite($fp, $sql . "\n\r");
fclose($fp);*/
	$query=mysql_query($sql);
	if($alert==0){
	    if($query) return 1;
	    else return mysql_error();
	}
	elseif($alert==1){
	    echo $sql;
	}
	elseif($alert==2){
		echo $sql;
	    if($query) return 1;
	    else return mysql_error();
	}

}

function inc_fanli($ddusername, $fxje, $tgje, $order_code,$merchant_id) {
	if(JIFENOPEN==1 && JIFENBL>0){
		$jifen=round($fxje*JIFENBL);
		if($jifen>0){
			$msg_tabao_jifen=$jifen."积分！";
		}
	}
	else{
		$jifen=0;
	}
	$field_arr=array('money'=>$fxje,'jifen'=>$jifen,'dengji'=>1);
	update_sql('user', $field_arr, "ddusername='$ddusername'", 1); //增加会员金额，积分和等级

	//用户消息
	$title = '您获得了新的商城交易返现！';
	$trade_id = $order_code;
	$msg_tabao = "您获得了新的交易返现，".$merchant_id."商城订单号" . $order_code . "返现金额" . $fxje.'！'.$msg_tabao_jifen;
	$filed_arr = array (
		'title' => $title,
		'content' => $msg_tabao,
		'addtime' => date('Y-m-d H:i:s'
	), 'see' => 0, 'ddusername' => $ddusername, 'senduser' => '网站客服');
	insert_one_sql("msg", $filed_arr);

	//用户明细
	$shijian = "商城交易返现";
	$memo = $merchant_id."交易号$order_code";
	$filed_arr = array (
		'ddusername' => $ddusername,
		'shijian' => $shijian,
		'addtime' => date('Y-m-d H:i:s'
	), 'je' => $fxje,'jifen'=>$jifen, 'memo' => $memo);
	insert_one_sql("mingxi", $filed_arr);

	//求推荐人
	$tjrid = sel_sql("user", "tjr", "ddusername='$ddusername'");
	if ($tjrid > 0) {
		$tjrname = sel_sql("user", "ddusername", "Id='$tjrid'");
		//增加推荐人佣金
		$field_arr = array (
			'money' => $tgje
		);
		update_sql("user", $field_arr, "Id='$tjrid'", 1);

		//用户消息
		$title = '您获得了新的推广佣金！';
		$msg_taobaotuiguang = "您获得了新的推广佣金" . $tgje;
		$filed_arr = array (
			'title' => $title,
			'content' => $msg_taobaotuiguang,
			'addtime' => date('Y-m-d H:i:s'
		), 'see' => 0, 'ddusername' => $tjrname, 'senduser' => '网站客服');
		insert_one_sql("msg", $filed_arr);

		//用户明细
		$shijian = "推广佣金";
		$memo = "交易人$ddusername";
		$filed_arr = array (
			'ddusername' => $tjrname,
			'shijian' => $shijian,
			'addtime' => date('Y-m-d H:i:s'
		), 'je' => $tgje, 'memo' => $memo);
		insert_one_sql("mingxi", $filed_arr);
	}
}

function create($dir) {
	if (!is_dir($dir)) {
		$temp = explode('/', $dir);
		$cur_dir = '';
		for ($i = 0; $i < count($temp); $i++) {
			$cur_dir .= $temp[$i] . '/';
			if (!is_dir($cur_dir)) {
				@ mkdir($cur_dir);
			}
		}
	}
}

function rep($str){
    $re="/[^\d]/";
    return preg_replace($re,"",$str);
}

$get=var_export($_GET, true);
$dirname ='../data/duomai/'. date("Y");
create($dirname);
$fp = fopen($dirname.'/'.date('md').'.php', 'a');
fwrite($fp, $get . "\n\r");
fclose($fp);

$ads_id=$_GET['ads_id'];//活动ID
$site_id=$_GET['site_id'];//网站ID
$link_id=$_GET['link_id'];//活动链接ID
$euid=$_GET['euid'];//	网站主设定的反馈标签
$order_sn=$_GET['order_sn'];//	订单编号
$order_time=date('Ymd His',strtotime($_GET['order_time']));//	下单时间
$orders_price=$_GET['orders_price'];//订单金额
$siter_commission=$_GET['siter_commission'];//	订单佣金
$confirm_siter_commission=$_GET['confirm_siter_commission'];//确认佣金

$status=$_GET['status'];//订单状态  -1 无效 0 未确认 1 确认 2 结算

$checksum_md5=md5($_GET['ads_id'].$_GET['ads_name'].$_GET['euid'].$_GET['link_id'].$_GET['order_sn'].$_GET['order_time'].$_GET['orders_price'].$_GET['site_id'].$_GET['siter_commission'].$_GET['status'].DUOMAIMIYUE);

if($checksum_md5!=$_GET['checksum']){
    exit('hack');
}

$merchant_id=sel_sql('mall','name','duomaiid='.$ads_id);

$order_code=$order_sn; //订单编号

$product_code=$order_sn; //商品编号多麦没的用订单编号替换
$prod_name=''; //商品名称多麦也没的
$sales =$orders_price; //总额多麦只有总的
$comm_type='cps'; //佣金类型
$commission=$siter_commission; //网站主佣金
//20120518修改过
if($confirm_siter_commission>0){
	$commission=$confirm_siter_commission;//确认佣金
}else{
	$commission=$siter_commission;//	订单佣金
}

$u_id=$euid?$euid:0; //反馈标签
$dduser = sel_sql("user", "ddusername,dengji", "Id='$u_id'");
$ddusername = $dduser['ddusername'];
$dengji = $dduser['dengji'];
$fxje = mall_ck_fxje($commission, $dengji); //会员返利金额
$tgje = round($commission * TGBL, 2); //给会员的推广佣金
$status=$status; //订单状态
switch($status){
    case '0': $stat_desc='未核对';
	break;
	case '1': $stat_desc='未核对';
	break;
	case '2': $stat_desc='核对有效';
	break;
	case '-1': $stat_desc='无效订单';
	break;
}

//添加交易记录
$mall_order = sel_sql("mall_order", "id,stat_desc,fxje,commission", "order_code='$order_code'"); //用订单编号查
if ($mall_order['id'] == '') { //交易不存在
	$field_arr = array (
		'adid' => $ads_id,
		'lm' => 'duomai',
		'order_time' => $order_time,
		'merchant_id' => $merchant_id,
		'u_id' => $u_id,
		'order_code' => $order_code,
		'item_count' => 1,
		'item_price' => $sales,
		'sales' => $sales,
		'commission' => $commission,
		'stat_desc' => $stat_desc,
		'fxje' => $fxje,
		'ddusername' => $ddusername,
		'bill_yyyymmdd'=>date('Y-m-d H:i:s')
	);
	if($stat_desc=='核对有效'){
	    $field_arr['qrsj']=date('Y-m-d H:i:s');
	}
	if($_GET['ads_id']!=0){
	    insert_one_sql("mall_order", $field_arr,0);
	}
	
	if($stat_desc=='核对有效'){
		if($ddusername!='' and $fxje>0){
		    inc_fanli($ddusername, $fxje, $tgje, $order_code, $merchant_id);
		}
	}
    echo 1;
}
elseif($mall_order['id']>0 and $mall_order['stat_desc']=='未核对' and $stat_desc=='核对有效'){
	$field_arr_order = array (
		'stat_desc' => '核对有效',
		'qrsj' => date('Y-m-d H:i:s'), 
		'fxje' => $fxje,
		'commission' => $commission,
		'bill_yyyymmdd' => $bill_yyyymmdd
	);
	update_sql('mall_order', $field_arr_order, "id='".$mall_order['id']."'",0);
	$tgje = round($commission * TGBL, 2);
	if($fxje>0){
	    inc_fanli($ddusername, $fxje, $tgje, $order_code, $merchant_id);
	}
	echo 0;
}
elseif($mall_order['id']>0 and $mall_order['stat_desc']=='未核对' and $stat_desc=='无效订单'){
	$field_arr_order=array('stat_desc'=>'无效订单','qrsj'=>date('Y-m-d H:i:s'));
	update_sql('mall_order', $field_arr_order, "id='".$mall_order['id']."'",0);
    echo 0;
}
else{
    echo 0;
}

mysql_close();
?>